package com.tencent.wxcloudrun.service.impl;

import com.tencent.wxcloudrun.dto.*;
import com.tencent.wxcloudrun.mapper.GoodsMapper;
import com.tencent.wxcloudrun.mapper.OrdergoodsMapper;
import com.tencent.wxcloudrun.mapper.SkulistMapper;
import com.tencent.wxcloudrun.service.OrdergoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/16 15:19
 * @description: TODO
 */
@Service
public class OrdergoodsServiceImpl implements OrdergoodsService {
    @Autowired
    private OrdergoodsMapper ordergoodsMapper;
    @Autowired
    private SkulistMapper skulistMapper;

    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<Ordergoods> ordergoodsList(String id) {
        return ordergoodsMapper.ordergoodsList(id);
    }

    @Override
    public int addOrderGoods(List<OrderCreateGoods> goodsJsonStr, String orderid) {
        int k = 0;
        for (int i = 0; i < goodsJsonStr.size(); i++) {
            String propertyChildIds = goodsJsonStr.get(i).getPropertyChildIds();
            Integer amout = 0;
            Integer score = 0;
            if (propertyChildIds == null) {
                Map<String, Integer> goodsPrices = skulistMapper.getGoodsPrices(goodsJsonStr.get(i).getGoodsId());
                amout = goodsPrices.get("minPrice");
                score = goodsPrices.get("minScore");
            }else {
                GoodsPrice price = skulistMapper.getGoodsPrice(goodsJsonStr.get(i).getGoodsId(), goodsJsonStr.get(i).getPropertyChildIds());
                amout = price.getPrice();
                score = price.getScore();
            }
            Goods goods = goodsMapper.getById(String.valueOf(goodsJsonStr.get(i).getGoodsId()));
            String pic = goods.getPic();
            ordergoodsMapper.addOrderGoods(goodsJsonStr.get(i).getGoodsId(), goodsJsonStr.get(i).getNumber(), orderid,amout,score,pic);
            List<GoodsAddition> goodsAdditionList = goodsJsonStr.get(i).getGoodsAdditionList();
            if (goodsAdditionList != null){
            for (int j = 0; j < goodsJsonStr.get(i).getGoodsAdditionList().size(); j++) {
                ordergoodsMapper.addOrderGoodsAddition(goodsJsonStr.get(i).getGoodsAdditionList().get(j).getId(), goodsJsonStr.get(i).getGoodsAdditionList().get(j).getPid(), orderid);
            }}
         k=k+1;
        }
        return k;
    }

    @Override
    public List<Ordergoods> getGoodsNumber(String orderid) {
        return ordergoodsMapper.getGoodsNumber(orderid);
    }
}
